package com.qifeng.modules.postInfo.repository;

import com.qifeng.modules.postInfo.domain.PostInfo;
import com.qifeng.modules.postInfo.domain.PostInfoVo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by if on 2017/11/22.
 */
@Repository
public interface PostInfoRepository extends JpaRepository<PostInfo, Long>, JpaSpecificationExecutor<PostInfo> {

    List<PostInfo> findByUserId(Long id);

    /**
     * 待审核
     */
    @Query(value = "SELECT pi FROM PostInfo pi where (select count(oi) from OrderInfo oi where pi.id = oi.postId) > 0")
    List<PostInfo> getReviewList();

    /**
     * 待完成
     *
     * @return
     */
    @Query(value = "SELECT new com.qifeng.modules.postInfo.domain.PostInfoVo(pi,(CASE WHEN pi.endTime >= current_timestamp THEN 1 WHEN pi.endTime < current_timestamp THEN 2 else 1 END))" +
            "FROM PostInfo pi WHERE pi.status <> 2")
    List<PostInfoVo> getCompleteList();

}

